package com.light.gulimall.seckill.schedule;

import com.light.gulimall.seckill.service.SecKillSkuService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class SeckillSkuSchedule {

    @Autowired
    private SecKillSkuService secKillSkuService;

    @Autowired
    private RedissonClient redissonClient;

    private final String UPLOAD_LOCK = "seckill:upload:lock";

    // 每天凌晨 3 点执行
    @Scheduled(cron = "0 0 3 * * *")
//    @Scheduled(cron = "* * * * * *")
    public void executeTaskAt3AMUpSeckillSku() {
        //加分布式锁，防止多实例重复执行
        RLock lock = redissonClient.getLock(UPLOAD_LOCK);
        try {
            lock.lock();
            secKillSkuService.uploadSecKillSkuInfo();
        } finally {
            lock.unlock();
        }
    }

}
